package cn.cms.dao;

import cn.cms.model.SysUser;
import org.jfaster.mango.annotation.DB;
import org.jfaster.mango.annotation.Rename;
import org.jfaster.mango.annotation.ReturnGeneratedId;
import org.jfaster.mango.annotation.SQL;

import java.util.List;

@DB(table = "sys_user")
public interface SysUserDao {

    @SQL("insert into #table (role_ids, real_name, login_name, " +
            "      encrypted_password, salt, email, " +
            "      telephone, department, register_date, " +
            "      level, state" +
            "      )" +
            "    values (:t.roleIds, :t.realName, :t.loginName, " +
            "      :t.encryptedPassword, :t.salt, :t.email, " +
            "      :t.telephone, :t.department, :t.registerDate, " +
            "      :t.level, :t.state" +
            "      )"
    )
    @ReturnGeneratedId
    public Long save(@Rename("t") SysUser sysUser);

    @SQL(
            "update #table" +
                    "    set " +
                    " role_ids = :t.roleIds, " +
                    " real_name = :t.realName, " +
                    " encrypted_password = :t.encryptedPassword, " +
                    "      salt = :t.salt, " +
                    "      email = :t.email, " +
                    "      telephone = :t.telephone, " +
                    "      department = :t.department, " +
                    "      level = :t.level, " +
                    "      state = :t.state " +
                    " where id=:t.id "
    )
    public void update(@Rename("t") SysUser sysUser);

    @SQL("update #table set encrypted_password=:encryptedPassword where id=:id")
    public void updatePwd(SysUser sysUser);

    @SQL("delete from #table where id=:1")
    public void delete(Long id);

    @SQL("delete from #table where id=:id")
    public void delete(SysUser sysUser);

    @SQL("select * from #table where id=:1")
    public SysUser get(Long id);

    @SQL("select * from #table where id=:id")
    public SysUser get(SysUser sysUser);

    @SQL("select * from #table where 1=1 " +
            " #if (:keyWords!=''&&:keyWords!=null) and login_name like :keyWords #end " +
            " #if (:keyWords!=''&&:keyWords!=null) or real_name like :keyWords #end " +
            " order by level asc, " +
            " register_date desc "
    )
    public List<SysUser> getList(PageDao pageDao, @Rename("keyWords") String keyWords);

    @SQL("select * from #table where login_name=:1 and encrypted_password=:2")
    public SysUser getUser(String loginName, String password);

    @SQL("select * from #table where login_name=:1 limit 1")
    public SysUser getByLoginName(String loginName);

}
